package com.haoyeteng.sxbd.core.mongodb;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

import com.mongodb.BasicDBObject;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;

/**
 * mongo文件读入
 * 
 * @author 理想
 *
 */
public class MongoFileReader {

	@Autowired
	MongoTemplate mongoTemplate;

	public GridFSDBFile toRead(String fileId) throws IOException {
		return toRead(fileId, null);
	}

	public GridFSDBFile toRead(String fileId, String fileName) throws IOException {
		GridFS gridFs = new GridFS(mongoTemplate.getDb());

		BasicDBObject query = new BasicDBObject();
		if (fileName != null) {
			query.put("filename", fileName);
		}
		query.put("_id", fileId);
		
		//List<GridFSDBFile> gridFSDBFile = gridFs.find(query);
		return gridFs.findOne(query);
	}

}